<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <title>故障信息</title>
    <link href="../../../css/common.css" rel="stylesheet"/>
    <link href="../../../css/mui.min.css" rel="stylesheet"/>
    <link href="../../../css/mui.picker.min.css" rel="stylesheet"/>
    <link href="../../../css/mui.poppicker.css" rel="stylesheet"/>
    <link href="../../../css/order/orderDetail.css" rel="stylesheet"/>


    <script src="../../../js/vue.min.js"></script>
    <script src="../../../js/mui.min.js"></script>
    <script src="../../../js/mui-componets.js"></script>
    <script src="../../../js/fastclick.js"></script>
    <script src="../../../js/vue-common.js"></script>
    <script src="../../../js/mui.picker.min.js"></script>
    <script src="../../../js/mui.poppicker.js"></script>
    <script src="../../../js/api.js"></script>
    <script src="../../../js/common.js"></script>

</head>
<body>
<div id="app">
    <div class="header-nav">
        <span>故障信息</span>
        <div class="icon"><img src="../../../img/common/btn_back.png" alt=""></div>
    </div>

    <div class="container">
        <div class="card-box order-info">
            <div class="order-title">机具信息</div>
            <div>
                <span class="label-weight">机具编号:</span>
                <span class="label-light">{{code}}</span>
            </div>

            <div>
                <span class="label-weight">机具类型:</span>
                <span class="label-light">{{device.typeName}}</span>
            </div>

            <div>
                <span class="label-weight">网点名称:</span>
                <span class="label-light">{{device.insSide}}</span>
            </div>
        </div>
        <div class="card-box device-info">
            <span class="label-light">报修类型</span>
            <ls-single-select :type-map="errorStatusMap" v-model="form.errorStatus"></ls-single-select>
        </div>

        <div class="card-box device-info">
            <span class="label-light ">故障模块</span>
            <ls-multi-select :type-map="errorTypeMap" v-model="form.errorType"></ls-multi-select>
        </div>

        <div class="card-box li">
            <div class="title">
                <span class="label-light ">特殊需求</span>
            </div>

            <div class="xlb-li">
                <div class="left">预约时间</div>
                <div id='yuyueTime' class="right">
                    <span id="yuyueTime_result" data-options='{}' style="text-align: center">无预约</span>
                    <img src="../../../img/common/btn_more.png" alt=""></span>
                </div>
            </div>
            <div class="xlb-li" @click="goContact">
                <div class="left">联系人</div>
                <div class="right">{{abutmentUserName}} <img src="../../../img/common/btn_more.png" alt=""></div>
            </div>

        </div>
        <div class="card-box li">
            <div class="card-box device-info">
                <span class="label-light ">故障照片</span>
                <!-- 已经上传的照片 -->
                <div class="img-list">
                    <div class="img-item" v-for="imgUrl in errorImgShow ">
                        <div class="img-close" @click="delImg(imgUrl)" @click="delImg(imgUrl)">X</div>
                        <img :src="imgUrl" @click="showImg(imgUrl)">
                    </div>
                    <div class="img-item">
                        <img @click="selectPhoto" src="../../../img/common/btn_addpic@3x.png" alt="">
                    </div>
                </div>
            </div>

            <div class="card-box device-info">
                <div class="order-title">故障描述</div>
                <div>
                    <textarea v-model="form.errorDescribe" class="text-area" placeholder="请详细描写机具出现的故障"></textarea>
                </div>

            </div>
        </div>
        <div v-if="imgShow" class="img-show-box">
            <div @click="closeImgShow" :class="{'img-show-box-shade':imgShow}"></div>
            <img  @click="closeImgShow" :src="imgShowUrl" alt="">
        </div>

        <div @touchstart="addOrder" :class="{disabled:submitFlag}" class="xl-btn normal no-shadow">提交</div>
    </div>

    <xlb-modal v-model="confirmMod" >
        <div class="dialog-content" slot="content">确认不填写故障描述吗？</div>
        <div slot="footer-left">取消</div>
        <div @click="doConfirm" slot="footer-right">确定</div>
    </xlb-modal>
</div>
</div>
<script>
    var vm = new Vue({
        el: '#app',
        data: {
            confirmMod:false,
            imgShow: false,
            imgShowUrl: '../../img/common/btn_addpic@3x.png',
            code: '',
            showFlg: false,
            submitFlag: false,

            /**
             * 机具编码
             */
            deviceCode: '',
            /**
             * 设备
             */
            device: {
                id: '',
                insSide: '',
                typeName: '',
            },
            /**
             * 故障模块map
             */
            errorTypeMap: {},
            /**
             * 故障图片展示
             */
            errorImgShow: [],
            /**
             * 故障状态map
             */
            errorStatusMap: {
                '1': '不能使用',
                '2': '可以使用',
            },
            //表单参数
            form: {

                /**
                 * 故障图片
                 */
                errorImg: [],
                /**
                 * 选择的故障模块
                 */
                errorType: [],
                /**
                 * 故障状态
                 */
                errorStatus: '',
                /**
                 * 故障描述
                 */
                errorDescribe: '',

                yuyueFlag: false,
                /**
                 * 预约时间
                 */
                dateDate: '',
                limitFlag: false,
                /**
                 * 限定完成时间
                 */
                limitDate: '',
                /**
                 * 联系人
                 */
                contactFlag: false,
                abutmentUserId: '',

            },
            abutmentUserName: '未填写',
            submitParams:''


        },
        methods: {
            //扫码
            scanCode: function () {
                redirect('pages/common/scanCode.html')
            },
            //手动输入code
            searchCode: function () {
                if (this.deviceCode) {
                    scaned('', this.deviceCode);
                }

            },
            getDeviceInfo: function (code) {
                Resource.post(DEVICE_API, {code: code}, function (res) {
                    //故障状态
                    vm.errorStatusMap = res.errorStatusMap;
                    //故障模块
                    vm.errorTypeMap = res.errorUnTypeMap;
                    //设备
                    vm.device = res.device;

                })
            },

            //上传照片
            selectPhoto: function () {
                const _self = this;
                uploadPhoto(function (data) {
                    _self.form.errorImg.push('' + data)
                    _self.errorImgShow.push(getImgServer() + data)
                })
            },

            delImg: function (imgUrl) {
                var imgServer = getImgServer();
                var img = imgUrl.replace(imgServer, '');
                this.form.errorImg.splice(this.form.errorImg.indexOf(img + ''), 1);
                this.errorImgShow.splice(this.errorImgShow.indexOf(imgUrl), 1);
            },
            showImg: function (img) {
                this.imgShow = true;
                this.imgShowUrl = img
            },
            closeImgShow: function () {
                this.imgShow = false;
            },
            //生成订单
            addOrder: function () {
                if (this.submitFlag) {
                    return;
                }
                if (!this.device.id) {//校验机具ID
                    return mui.toast("请选择报修设备");
                }
                if (!this.form.errorStatus) {
                    return mui.toast("请选择报修类型");
                }
                if (!this.form.errorType.length) {
                    return mui.toast("请选择故障模块");
                }
                if (this.form.limitFlag && (!this.form.limitDate)) {
                    return mui.toast("请填写限定完成时间");
                }
                if (this.form.yuyueFlag && (!this.form.dateDate)) {
                    return mui.toast("请填写预约时间");
                }
                if (this.form.contactFlag && (!this.form.abutmentUserId)) {
                    return mui.toast("请填写联系人");
                }

                this.form.id = this.device.id;
                var params = {
                    deviceId: this.device.id,
                    /**
                     * 故障图片
                     */
                    errorImg: this.form.errorImg + '',
                    /**
                     * 选择的故障模块
                     */
                    errorType: this.form.errorType + '',
                    /**
                     * 故障状态
                     */
                    errorStatus: this.form.errorStatus,
                    /**
                     * 故障描述
                     */
                    errorDescribe: this.form.errorDescribe,
                    /**
                     * 预约时间
                     */
                    dateDate: this.form.dateDate,
                    /**
                     * 限定完成时间
                     */
                    limitDate: this.form.limitDate,
                    /**
                     * 联系人
                     */
                    abutmentUserId: this.form.abutmentUserId,

                }

                if (!this.form.errorDescribe) {
                    // var btnArray = ['填写', '提交'];
                    // mui.confirm('建单人备注未填写？', '提示', btnArray, function (e) {
                    //     if (e.index == 1) {
                    //         vm.doSaveOrder(params);
                    //     }
                    // })
                    this.confirmMod = true;
                    this.submitParams = params;
                } else {
                    vm.doSaveOrder(params);
                }


            },
            doConfirm: function(){
                if (this.submitFlag) {
                    return;
                }
                if (!this.device.id) {//校验机具ID
                    return mui.toast("请选择报修设备");
                }
                if (!this.form.errorStatus) {
                    return mui.toast("请选择报修类型");
                }
                if (!this.form.errorType.length) {
                    return mui.toast("请选择故障模块");
                }
                if (this.form.limitFlag && (!this.form.limitDate)) {
                    return mui.toast("请填写限定完成时间");
                }
                if (this.form.yuyueFlag && (!this.form.dateDate)) {
                    return mui.toast("请填写预约时间");
                }
                if (this.form.contactFlag && (!this.form.abutmentUserId)) {
                    return mui.toast("请填写联系人");
                }

                this.form.id = this.device.id;
                var params = {
                    deviceId: this.device.id,
                    /**
                     * 故障图片
                     */
                    errorImg: this.form.errorImg + '',
                    /**
                     * 选择的故障模块
                     */
                    errorType: this.form.errorType + '',
                    /**
                     * 故障状态
                     */
                    errorStatus: this.form.errorStatus,
                    /**
                     * 故障描述
                     */
                    errorDescribe: this.form.errorDescribe,
                    /**
                     * 预约时间
                     */
                    dateDate: this.form.dateDate,
                    /**
                     * 限定完成时间
                     */
                    limitDate: this.form.limitDate,
                    /**
                     * 联系人
                     */
                    abutmentUserId: this.form.abutmentUserId,

                }
                vm.doSaveOrder(params);

            },
            doSaveOrder: function (params) {
                if(!params){
                    params= this.submitParams;
                }
                this.submitFlag = true;
                this.btnTitle = '提交中',
                    Resource.post(ORDER_ADD_API, params, function (res) {
                        mui.toast('报修成功！')
                        vm.showFlg = false;
                        vm.submitFlag = false;
                        //跳转订单页面
                        redirectMainPage('pages/order/list.html');
                    },function () {
                        vm.showFlg = false;
                        vm.submitFlag = false;
                    })
            },
            //重置参数
            resetForm: function () {
                this.deviceCode = '';
                this.device = {
                    id: '',
                    insSide: '',
                    typeName: '',
                };
                this.errorTypeMap = {};
                this.errorImgShow = [];
                this.errorStatusMap = {};
                this.form = {
                    errorImg: [],

                    errorType: [],

                    errorStatus: '',

                    errorDescribe: '',
                    dateDate: '',

                    limitDate: '',

                    abutmentUserId: '',

                };
            },
            goContact: function () {
                redirect('pages/order/contact/index.html')
            }
        }
    });


</script>
<script>

    selectTime('yuyueTime', function (data) {
        vm.form.dateDate = data;
    }, '预约时间: ');//预约时间
</script>
<script>
    function selectContactUser() {
        Resource.get(ORDER_ADD_CONTACT_API, {}, function (res) {
            /**
             * "abutmentUserId":26,"abutmentUserName":"JackWu",
             * 获取对象属性的值
             * 主要用于过滤三级联动中，可能出现的最低级的数据不存在的情况，实际开发中需要注意这一点；
             * @param {Object} obj 对象
             * @param {String} param 属性名
             */
            var _getParam = function (obj, param) {
                return obj[param] || '';
            };
            //普通示例
            var userPicker = new mui.PopPicker();
            var listUser = [];
            for (var index in res) {
                listUser.push({
                    value: res[index]['abutmentUserId'],
                    text: res[index]['abutmentUserName']
                })
            }
            userPicker.setData(listUser);
            var showUserPickerButton = document.getElementById('showUserPicker');
            var userResult = document.getElementById('showUserPicker');
            showUserPickerButton.addEventListener('tap', function (event) {
                userPicker.show(function (items) {
                    userResult.innerText = "对接人： " + items[0].text;
                    vm.form.abutmentUserId = items[0].value;
                    //返回 false 可以阻止选择框的关闭
                    //return false;
                });
            }, false);
        })
    }

    mui.plusReady(function (data) {
        var self = plus.webview.currentWebview();
        vm.code = self.code;
        vm.getDeviceInfo(self.code);
        vm.showFlg = false;
    });

    document.addEventListener('changeTab', function (data) {
        vm.imgServer = getImgServer();
        vm.resetForm()
        vm.showFlg = false;
        var code = data.detail.params.code;
        vm.getDeviceInfo(code);

    })

    // 处理选择联系人
    function dealSelectContact(data) {
        data = JSON.parse(data);
        vm.abutmentUserName = data.name;
        if (data.type == 1) {
            vm.form.abutmentUserId = data.id;
        }
    }
    mui.init({
        beforeback: function () {
            if(vm.imgShow){
                return vm.imgShow = false;
            }
            return true;
        }
    });
</script>

</body>
</html>
